package com.spn.blog.article.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class MQ {
    public static final String DELETE_EXCHANGE = "delete.exchange";
    public static final String DELETE_QUEUE = "delete.queue";
    public static final String DEAD_LETTER_EXCHANGE = "dead.exchange";
    public static final String DEAD_LETTER_QUEUE = "dead.queue";
    //声明业务 Exchange
    @Bean(DELETE_EXCHANGE)
    public DirectExchange confirmExchange(){
        return new DirectExchange(DELETE_EXCHANGE);
    }

    @Bean(DEAD_LETTER_EXCHANGE)
    public DirectExchange deadExchange(){
        return new DirectExchange(DEAD_LETTER_EXCHANGE);
    }
    // 声明队列
    @Bean(DELETE_QUEUE)
    public Queue getDeleteQueue(){
        Map<String,Object> params=new HashMap<>();
        params.put("x-dead-letter-exchange",DEAD_LETTER_EXCHANGE);
        params.put("x-dead-letter-routing-key","dead");
        params.put("x-message-ttl", 10000);
        return QueueBuilder.nonDurable(DELETE_QUEUE).withArguments(params).build();
    }

    @Bean(DEAD_LETTER_QUEUE)
    public Queue getDeadQueueD(){
        return QueueBuilder.nonDurable(DEAD_LETTER_QUEUE).build();
    }

    // 声明队列绑定关系
    @Bean
    public Binding confirmQueueBinding(@Qualifier(DELETE_QUEUE) Queue queue,
                                       @Qualifier(DELETE_EXCHANGE) DirectExchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with("delete");
    }

    @Bean
    public Binding QueueDBindToExchangeY(@Qualifier(DEAD_LETTER_QUEUE) Queue queue,
                                         @Qualifier(DEAD_LETTER_EXCHANGE) DirectExchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with("dead");
    }
}
